/// [Largest palindrome product](https://projecteuler.net/problem=4)
fn main() {
    for digit in &[2, 3] {
        let value = largest_palindrome_product(*digit);
        println!(
            "The largest palindrome made from the product of two {}-digit numbers is {} = {} X {}.",
            digit,
            value.0 * value.1,
            value.0,
            value.1,
        );
    }
}
/// 解法：从小到大遍历，有更好的方法
fn largest_palindrome_product(digit: u32) -> (u32, u32) {
    let base = 10_u32.pow(digit - 1);
    let mut idx_max = (0, 0);
    let mut mul_max = 0;
    for a in base..base * 10 {
        for b in base..=a {
            let mul_new = a * b;
            if mul_new > mul_max {
                let mut tmp = mul_new.to_string();
                unsafe {
                    let vec = tmp.as_mut_vec();
                    vec.reverse();
                }
                let rev: u32 = tmp.parse().unwrap();
                if mul_new == rev {
                    mul_max = mul_new;
                    idx_max = (b, a);
                }
            }
        }
    }
    idx_max
}
